package handler

import (
	"factory-mgt-sys/moder"

	"github.com/gin-gonic/gin"
)

func Add(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"mas":  "不能为空",
			"code": 500,
		})
		return
	}

	user := moder.User{
		Username: username,
		Password: password,
	}

	err := moder.DB.Create(&user).Error

	if err != nil {
		c.JSON(200, gin.H{
			"mas":  "添加失败",
			"code": 500,
		})
		return
	}

	c.JSON(200, gin.H{
		"mas":  "添加成功",
		"code": 200,
	})
	return

}

func List(c *gin.Context) {
	var User []moder.User
	err := moder.DB.Find(&User).Error

	if err != nil {
		c.JSON(200, gin.H{
			"mas":  "获取数据失败",
			"code": 500,
		})
		return
	}

	c.JSON(200, gin.H{
		"mas":  "获取数据成功",
		"code": 200,
		"data": User,
	})
	return

}
